package com.tiyende.kash.loop.weight.city

import java.util.Locale
import java.util.regex.Pattern

/**
 * Created by YoKey on 16/3/20.
 */
object CityDataUtil {
    private const val PATTERN_POLYPHONE = "^#[a-zA-Z]+#.+"
    private const val PATTERN_LETTER = "^[a-zA-Z].*+"

    /**
     * cityName -> Lowercase
     */
    fun getCityNameLowercase(inputString: String?): String {
        return inputString?.lowercase(Locale.getDefault()) ?: ""
    }

    /**
     * Are start with a letter
     *
     * @return if return false, index should be #
     */
    fun matchingLetter(inputString: String?): Boolean {
        return Pattern.matches(PATTERN_LETTER, inputString)
    }

    fun matchingPolyphone(inputString: String?): Boolean {
        return Pattern.matches(PATTERN_POLYPHONE, inputString)
    }

    fun gePolyphoneInitial(inputString: String): String {
        return inputString.substring(1, 2)
    }

    fun getPolyphoneRealLowercase(inputString: String): String {
        val splits = inputString.split("#".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        return splits[1]
    }

    fun getPolyphoneRealHanzi(inputString: String): String {
        val splits = inputString.split("#".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        return splits[2]
    }
}
